<template>
  <div>
    <ul class="flex flex-wrap h-300px">
      <li
        v-for="(item, key) in addonList"
        :key="key"
        :class="[
          'cursor-pointer relative w-[calc(12.5%-10px)] m-5px transition-all duration-500 ease-out',
          { choose: chooseAddonValue.hasOwnProperty(item.name) }
        ]"
        @click="onChooseAddon(item)"
      >
        <div class="relative h-110px flex rounded-10px overflow-hidden hover:brightness-90 group">
          <el-image :src="item.icon" alt="" fit="contain" class="w-full" />
          <div class="hidden absolute inset-0 text-center leading-110px text-30px bg-[rgba(0,0,0,0.6)] text-white [li.choose_&]:block">
            <el-icon><Check /></el-icon>
          </div>
          <div
            v-if="item.auth.length > 0"
            class="hidden absolute bottom-0 right-0 p-5px leading-14px text-white group-hover:block bg-[#000] left-0"
            style="background: var(--el-color-primary)"
            @click.stop="onSetAddonAuth(item)"
          >
            <div class="flex items-center justify-center">
              <el-icon><Setting /></el-icon>权限配置
            </div>
          </div>
        </div>
        <div class="p-5px text-12px text-center truncate">
          {{ item.title }}
        </div>
      </li>
    </ul>
    <el-pagination
      background
      layout="total,prev, pager, next"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      @current-change="currentChange"
    />

    <!-- 权限配置抽屉 -->
    <dk-auth-drawer
      v-model="dialogAuthVisible"
      :auth-item="setAuthItem"
      :default-checked-keys="setAuthDefaultCheckedKeys"
      @auth-change="authCheckChange"
    />
  </div>
</template>

<script setup>
import { ref, watch, onMounted, defineExpose } from 'vue'
import { getAddonsList } from '@/api/customer/addons'
import { getPlaid } from '@/utils/auth'
import { Setting, Check } from '@element-plus/icons-vue'
import DkAuthDrawer from './dk-auth-drawer.vue'

defineOptions({
  name: 'DkChooseAddon'
})

const props = defineProps({
  addonValue: {
    type: [Object, Array],
    default: () => ({})
  },
  platform: {
    type: Boolean,
    default: false
  }
})

const dialogAuthVisible = ref(false)
const page = ref(1)
const pageSize = ref(16)
const total = ref(0)
const addonList = ref([])
const searchData = ref({})
const chooseAddonItems = ref({}) // 选中的应用列表
const chooseAddonValue = ref({ ...props.addonValue }) // 最终输出的
const setAuthItem = ref({}) // 设置当前应用的权限
const setAuthDefaultCheckedKeys = ref([]) // 当前选择的默认权限

// 模块权限改变响应
const authCheckChange = (item, checkedKeys) => {
  chooseAddonValue.value[item.name] = checkedKeys
  chooseAddonItems.value[item.name] = item
}

// 设置当前的模块权限
const onSetAddonAuth = (item) => {
  dialogAuthVisible.value = true
  setAuthItem.value = item
  setAuthDefaultCheckedKeys.value = chooseAddonValue.value[item.name] || []
}

// 选择应用
const onChooseAddon = (item) => {
  if (chooseAddonValue.value[item.name] !== undefined) {
    delete chooseAddonValue.value[item.name]
    delete chooseAddonItems.value[item.name]
  } else {
    chooseAddonValue.value[item.name] = ['all']
    chooseAddonItems.value[item.name] = item
  }
}

// 刷新列表
const refreshList = () => {
  chooseAddonValue.value = {}
  chooseAddonItems.value = {}
}

// 分页
const currentChange = (newPage) => {
  page.value = newPage
  getList(newPage)
}

// 获取列表
const getList = (currentPage, type = 0) => {
  addonList.value = []
  page.value = currentPage || page.value

  if (props.platform) {
    searchData.value.plaid = getPlaid()
  }

  getAddonsList(page.value, pageSize.value, searchData.value, type).then((res) => {
    addonList.value = res.data
    total.value = parseInt(res.msg)
  })
}

// 组件挂载时获取列表
onMounted(() => {
  getList()
})

// 向父组件暴露方法和属性
defineExpose({
  refreshList,
  chooseAddonValue: chooseAddonValue.value,
  chooseAddonItems: chooseAddonItems.value
})
</script>

<style>
li.choose .mark {
  display: block;
}

.cover:hover .hover-op {
  display: block;
}
</style>
